/* Copyright (c) 2009 Lintorus@EU-Destromath
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package authenticator;



public class Util {

  public static byte[] longToByteArray(long a_long) {
    byte[] result = new byte[8];
    for (int i = 0; i < 8; i++) {
      result[i] = (byte)(a_long >>> (56 - 8*i));
    }
    return result;
  }

  public static int byteArrayToInt(byte[] a_byteArray, int a_position) {
    return (((int)(a_byteArray[a_position]) & 0xFF) << 24) +
           (((int)(a_byteArray[a_position + 1]) & 0xFF) << 16) +
           (((int)(a_byteArray[a_position + 2]) & 0xFF) << 8) +
           ((int)(a_byteArray[a_position + 3] & 0xFF));
  }

  public static long byteArrayToLong(byte[] a_byteArray) {
    return (((long)(a_byteArray[0]) & 0xFFL) << 56) +
           (((long)(a_byteArray[1]) & 0xFFL) << 48) +
           (((long)(a_byteArray[2]) & 0xFFL) << 40) +
           (((long)(a_byteArray[3]) & 0xFFL) << 32) +
           (((long)(a_byteArray[4]) & 0xFFL) << 24) +
           (((long)(a_byteArray[5]) & 0xFFL) << 16) +
           (((long)(a_byteArray[6]) & 0xFFL) << 8) +
           ((long)(a_byteArray[7]) & 0xFFL);
  }

  public static String byteArrayToHexString(byte[] a_byteArray) {
    String result = "";
    for (int i = 0; i < a_byteArray.length; i++) {
      String currentByteAsString = Integer.toHexString(a_byteArray[i] & 0xff);
      if (currentByteAsString.length() < 2) {
        result = result + "0";
      }
      result = result + currentByteAsString;
    }
    return result;
  }

  public static byte[] hexStringToByteArray(String a_string) {
    byte[] result = new byte[(a_string.length() + 1) / 2];
    for (int i = result.length - 1; i >= 0; i--) {
      result[i] = (byte)(Integer.parseInt(a_string.substring(Math.max(0, a_string.length() - 2)), 16));
      a_string = a_string.substring(0, Math.max(0, a_string.length() - 2));
    }
    return result;
  }


  /**
   *  Convert a char to a byte but with appropriate mapping to exclude I,L,O and S. E.g. A=10 but J=18 not 19 (as I is missing)
   */
  public static byte convertRestoreCodeCharToByte(char c)
  {
          if (c >= '0' && c <= '9')
          {
                  return (byte)(c - '0');
          }
          else
          {
                  byte index = (byte)(c + 10 - 65);
                  if (c >= 'I')
                  {
                          index--;
                  }
                  if (c >= 'L')
                  {
                          index--;
                  }
                  if (c >= 'O')
                  {
                          index--;
                  }
                  if (c >= 'S')
                  {
                          index--;
                  }

                  return index;
          }
  }

  /**
   Convert a byte to a char but with appropriate mapping to exclude I,L,O and S.
  */
  public static char convertRestoreCodeByteToChar(byte b)
  {
          int index = b & 0x1f;
          if (index <= 9)
          {
                  return (char)(index + 48);
          }
          else
          {
                  index = (index + 65) - 10;
                  if (index >= 73)
                  {
                          index++;
                  }
                  if (index >= 76)
                  {
                          index++;
                  }
                  if (index >= 79)
                  {
                          index++;
                  }
                  if (index >= 83)
                  {
                          index++;
                  }
                  return (char)index;
          }
  }
}
